package ui.data
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class HoverButton extends Sprite
	{
		private static const defaultPoint:Point = new Point(0, 0);
		
		private var canvas:BitmapData;
		private var bitmap:Bitmap;
		private var defaultRectangle:Rectangle;
		private var hoverRectangle:Rectangle;
		private var sheet:BitmapData;
		
		public function HoverButton() {}
		
		public function draw():void {
			canvas = new BitmapData(defaultRectangle.width, defaultRectangle.height, true);
			canvas.copyPixels(sheet, defaultRectangle, defaultPoint);
			
			bitmap = new Bitmap(canvas);
			addChild(bitmap);
		}
		
		public function addListeners():void {
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
		}
		
		private function onMouseOver(event:MouseEvent):void {
			removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			
			canvas.fillRect(defaultRectangle, 1);
			canvas.copyPixels(sheet, hoverRectangle, defaultPoint);
		}
		
		private function onMouseOut(event:MouseEvent):void {
			removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			
			canvas.fillRect(defaultRectangle, 1);
			canvas.copyPixels(sheet, defaultRectangle, defaultPoint);
		}
		
		public function setDefaultRectangle(defaultRectangle:Rectangle):void {
			this.defaultRectangle = defaultRectangle;
		}
		
		public function setHoverRectangle(hoverRectangle:Rectangle):void {
			this.hoverRectangle = hoverRectangle;
		}
		
		public function setSheet(sheet:BitmapData):void {
			this.sheet = sheet;
		}
	}
}